<#include "/common/_header.html" />
<style type="text/css">
	.infinite-scroll-preloader {margin-top:-1rem;}
	.floatBtn { background: #f16522;display: block;  position: fixed;  right: 15px;  bottom: 10px;  width: 45px;  height: 45px; border: 1px solid #f16522;  box-sizing: border-box;  -webkit-box-sizing: border-box;  border-radius: 50%;  -webkit-border-radius: 50%;  -moz-border-radius: 50%;  font-size: 12px;  color: #999999;  z-index: 99;  text-align: center; }
	.floatBtn>p{ margin-top: -5px;color:#fff;}
	.floatBtn>i{margin-top: 1rem;color:#fff;font-size:1.5rem;}
	.floatBtn:active{ background:#f16522; color: #fff; border: 1px solid #fff;}
	.swiper-slide {font-size: 0.8rem;background-color: #eee;padding-left:0.2rem;padding-right:0.2rem;text-align: center;display: -webkit-box;display: -ms-flexbox;display: -webkit-flex;display: flex;}
	.swiper-slide>span{font-size: 1rem;}
	.swiper-slide>a{font-size: 0.8rem;color:#000;margin-top:0.1rem;}
</style>
<div class="page-group" style="background:#fff;"><!-- 页面组 -->
	<div class="page page-current" style="background-color:#fff;"><!-- 页面组中的每一个页面，可以互相跳转 -->
		<header class="bar bar-nav"><!-- 每个页面的头部 -->
<!-- 			<a class="button button-link button-nav pull-left" onclick="refreshBbs()">
				<span class="iconfont icon-refresh" style="font-size: 1.5rem;"></span>
			</a> -->
			<h1 class="title" style="font-size: 1.1rem;font-weight:bold;">${uservo.userCommunityName}</h1>
			<input type="hidden" value="${category!1}" id="bbscategory">
		</header>
		<div class="content infinite-scroll-bottom" data-distance="100"><!-- 每个页面的内容区域 -->
 			<div style="margin-left:0.6rem;margin-top: 0.2rem;">
 			<#if tags?? && (tags?size > 0)>
		  		<#list tags as vvo>
		  			<#if vvo.tagname=="同乡汇">
		  				<span class="badge" style="margin-top: 0.2rem;margin-right:0.2rem;font-size:0.8rem;
	    				<#if vvo.id=category>background-color:#f16522;color:#fff<#else>background-color:#fff;color:#f16522;border:1px solid #f16522;</#if>" tagid="${vvo.id}" onclick="filterBbs(this)">${vvo.tagname}-${uservo.province}${uservo.city}(${uservo.sameHometownCount})</span>
		  			<#else>
		  				<span class="badge" style="margin-top: 0.2rem;margin-right:0.2rem;font-size:0.8rem;
	    				<#if vvo.id=category>background-color:#f16522;color:#fff<#else>background-color:#fff;color:#f16522;border:1px solid #f16522;</#if>" tagid="${vvo.id}" onclick="filterBbs(this)">${vvo.tagname}</span>
		  			</#if>
	    		</#list>
    		</#if>
 			</div>
 			<div style="margin-left:0.6rem;margin-top: 0.6rem;margin-right: 0.6rem;">
	 			<div class="swiper-container" style="height:0.9rem;width:auto;">
				    <div class="swiper-wrapper">
				      <#if convs?? && (convs?size > 0)>
				      	<#list convs as vvo>
				      		<div class="swiper-slide"><span class="iconfont icon-notification" style="color:#f16522;margin-right:0.1rem;"></span><a onclick="showAttation('${vvo.bbsid}','${vvo.content}')">${vvo.contentShort}</a></div>
				        </#list>
				      <#else>
				      	<div class="swiper-slide"><span class="iconfont icon-notification" style="color:#f16522;margin-right:0.1rem;"></span>暂无公告</div>
				      </#if>
				    </div>
			    </div>
 			</div>
 			<div class="card-list"></div>
			<div class="infinite-scroll-preloader">
              <div class="preloader"></div>
            </div>
		</div>
		<a href="javascript:void(0)" class="floatBtn" onclick="addBbs()">
		    <i class="iconfont icon-edit"></i>
		</a>
	</div>
</div>
<div id="hiddenForm"></div>
<#include "/common/_footer.html" />
<script src="${path}/resource/js/dateUtil.js"></script>
<script src="${path}/resource/js/layer/layer.js"></script>
<script>
$(function(){
	//加载flag
	var loading = false;
	$('.infinite-scroll-preloader').hide();
	// 最多可加载的条目
	var maxItems = 50;
    //预先加载10条
    $.showIndicator();
	//加载第一批帖子
	$(".infinite-scroll-bottom .card-list").empty().load("${path}/communityChat/list?category="+$("#bbscategory").val(),function(){
		$.hideIndicator();
		parsePrettyDate();
	});
	// 上次加载的序号
	var lastIndex = 10;
	// 注册'infinite'事件处理函数
	$(document).on('infinite', '.infinite-scroll-bottom',function() {
	    // 如果正在加载，则退出
	    if (loading) return;
	    // 设置flag
	    loading = true;
        if (lastIndex >= maxItems) {
            // 加载完毕，则注销无限加载事件，以防不必要的加载
            $.detachInfiniteScroll($('.infinite-scroll'));
            // 删除加载提示符
            $('.infinite-scroll-preloader').remove();
            //$.toast("没有更多内容了");
            return;
        }
        $('.infinite-scroll-preloader').show();
        var currentPage = (parseInt(lastIndex)/10)+1;
	    $.ajax({
			type:'post',
			url:'${path}/communityChat/list',
			data:{'currentPage':currentPage,'category':$("#bbscategory").val()},
			success:function(data){
				//var dataJson = JSON.parse(data);
				maxItems = $($("input[name='_totalRow']")[0]).val();
		        // 添加新条目
		        $('.infinite-scroll-bottom .card-list').append(data);
		        // 更新最后加载的序号
		        lastIndex = $('.card-list .card').length;
		        //容器发生改变,如果是js滚动，需要刷新滚动
		        $.refreshScroller();
		     	// 重置加载flag
		        loading = false;
		        parsePrettyDate();
		    },
			error:function(xhr,errorType , error) {
				console.log("sorry error\r\n");
			}
	    });
	});
	$(".swiper-container").swiper({
		direction: 'vertical',
		//loop: true,
		//autoplay: 3000,
		spaceBetween: 20
	});
	
});
function sendreply(obj,bbsid){
	var reply = $(obj).prev().val();
	if(reply.trim()==''){
		$.toast("请填写评论内容");
		return ;
	}
	var replylist = $(obj).parent().prev().children();
	$.showIndicator();
	$.ajax({
		type:'post',
		url:'${path}/communityChat/addComment',
		data:{"comments.bbsid":bbsid,"comments.comment":reply},
		success:function(data){
			if(replylist.length>0){
				if(replylist.length>=5){
					replylist[replylist.length-1].remove();
				}
				replylist.first().before("<div>${uservo.nickname}: "+reply+"</div>");
			}else{
				$(obj).parent().prev().append("<div>${uservo.nickname}: "+reply+"</div>");
			}
			$(obj).prev().val("");
			var cucount = parseInt($(obj).parent().parent().prev().find(".comcount").text());
			if(cucount==0){
				$(obj).parent().parent().prev().find(".comcount").prev().removeClass("iconfont icon-comment");
				$(obj).parent().parent().prev().find(".comcount").prev().addClass("iconfont icon-commentfill");
			}
			$(obj).parent().parent().prev().find(".comcount").text(cucount+1);
			cucount = parseInt($(obj).parent().parent().prev().find(".activeCount").text());
			if(cucount==0){
				$(obj).parent().parent().prev().find(".activeCount").prev().removeClass("iconfont icon-attention");
				$(obj).parent().parent().prev().find(".activeCount").prev().addClass("iconfont icon-attentionfill");
			}
			$(obj).parent().parent().prev().find(".activeCount").text(cucount+1);
			console.log("ajax success:"+data);
			$.hideIndicator();
		},
		error:function(xhr,errorType , error) {
			console.log("sorry error\r\n");
		}
	});
}
function sendyes(obj,bbsid){
	var yes = 1;
	if($(obj).children("i").attr('class')=='iconfont icon-like'){
		$(obj).children("i").removeClass("iconfont icon-like");
		$(obj).children("i").addClass("iconfont icon-likefill");
	}else{
		return;
	}
	$.showIndicator();
	$.ajax({
		type:'post',
		url:'${path}/communityChat/addYes',
		data:{"sayyes.yes_targetid":bbsid,"sayyes.yes_target_type":1,"sayyes.sayyes":yes},
		success:function(data){
			var cucount = parseInt($(obj).children(".yescount").text());
			if(yes){
				$(obj).children(".yescount").text(cucount+1);
				var account = parseInt($(obj).parent().children(".activeCountA").children(".activeCount").text());
				if(account==0){
					$(obj).parent().children(".activeCountA").children(".activeCount").prev().removeClass("iconfont icon-attention");
					$(obj).parent().children(".activeCountA").children(".activeCount").prev().addClass("iconfont icon-attentionfill");
				}
				$(obj).parent().children(".activeCountA").children(".activeCount").text(account+1);
				var footObj = $(obj).parent().parent().next();
				if(cucount==0){
					var htmlStr = '<div class="yeslist"><span class="iconfont icon-likefill" style="color:#f16522;margin-right:0.2rem;"></span>'+
					'<span class="yesNameStr">${uservo.nickname}</span></div><hr style="border-color: rgba(204, 204, 204, 0.2);">';
					footObj.children().first().before(htmlStr);
				}else{
					var yesListObj = footObj.find(".yesNameStr");
					yesListObj.text(yesListObj.text()+", ${uservo.nickname}");
				}
				//$(".yeslist").html($(".yeslist").html()+", ${uservo.nickname}");
			}else{
				//$(obj).children(".yescount").text(cucount-1);
			}
			$.hideIndicator();
		},
		error:function(xhr,errorType , error) {
			console.log("sorry error\r\n");
		}
	});
}
function showpic(imgsrc,bbsid,obj){
	layer.open({
	  type: 1
	  ,content: '<div onclick="javascript:layer.closeAll();" style="width:100%; height:100%;"><img src="'+imgsrc+'" style="max-width:100%;height:auto;"></div>'
	  ,style: 'background-color:#000000;display:flex;justify-content:center;align-items:center;position:fixed;left:0;top:0;width:100%; height:100%; border: none; -webkit-animation-duration: .5s; animation-duration: .5s;'
	});
	//add active count
    $.ajax({
		type:'post',
		url:'${path}/communityChat/clickPicture',
		data:{"bbsid":bbsid},
		success:function(data){
			if(data=="OK"){
				var cucount = parseInt($(obj).parent().parent().children(".row").children(".activeCountA").children(".activeCount").text());
				if(cucount==0){
					$(obj).parent().parent().children(".row").children(".activeCountA").children(".activeCount").prev().removeClass("iconfont icon-attention");
					$(obj).parent().parent().children(".row").children(".activeCountA").children(".activeCount").prev().addClass("iconfont icon-attentionfill");
				}
				$(obj).parent().parent().children(".row").children(".activeCountA").children(".activeCount").text(cucount+1);
			}else{
				console.log("Error.");
			}
		},
		error:function(xhr,errorType , error) {
			console.log("sorry error\r\n");
		}
	});
}
function addBbs(){
	var userlevel = "${uservo.userlevel}";
	if((userlevel=="1" || userlevel=="2" || userlevel=="3") && 
		("${uservo.checkinToday?string('1', '0')}"=="0")){
		$.alert("您目前的等级是${uservo.userlevelStr}，需要先定位打卡后才能发帖。点击确定开始定位",function(){
			$("#hiddenForm").html("<form action='${path}/checkin/selectcommunity' method='post' style='display:none;'>"+
			"<input type='hidden' value='1' name='fromAddBbs'></form>");
			$("#hiddenForm form").submit();
			//location.href='${path}/checkin/selectcommunity';
		});
	}else{
		location.href = "${path}/communityChat/add";
	}
}
function filterBbs(obj){
	location.href="${path}/communityChat?category="+$(obj).attr("tagid");
}
function showAttation(bbsid,content){
	var popupHTML = '<div class="popup close-popup" style="background-color:#fff;">'+
    '<div class="content-block">'+
    '<p style="font-size:1rem;">小区公告</p>'+
	'<p>'+content+'</p>'+
	'<p>&nbsp;</p>'+
	'<p><a href="#" class="close-popup pull-right button button-fill"><span class="iconfont icon-close"></span>关闭窗口</a></p>'+
    '</div></div>'
	$.popup(popupHTML);
    $(".close-popup").css("display","flex");
}
/*
function deletebbs(bbsid){
	$.confirm('确认删除', function () {
		location.href="${path}/communityChat/deleteEntity?bbsid="+bbsid;
	});
}
function refreshBbs(){
	location.href = "${path}/communityChat";
}
*/
</script>